package com.partners.controller;

import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.configure.wechatmp.WxMpConfiguration;
import com.partners.constant.ErrorMessageEnum;
import com.partners.constant.SuccessMessageEnum;
import com.partners.dao.bo.TaskDO;
import com.partners.dao.dto.TaskInputDTO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.TaskService;
import com.partners.utils.ApiResultUtil;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 任务管理
 * @author liangyongle
 * @date 2018-12-24
 */
@RestController
@RequestMapping("task")
@Slf4j
public class TaskController {

    public final static String appId = "wx1b38cf3729aaa2a7";

    @Resource
    private TaskService taskService;


    /**
     * 获取我的任务列表
     * @param uid  Integer
     * return list
     * */
    @PostMapping("getMyTask")
    public ApiResult getMyTaskList(@BeanValid @RequestBody Paging paging,
                                   @SimpleParam("uid") Integer uid){
        Map<String,Object> list =  taskService.getMyTaskList(paging,uid);
        return ApiResultUtil.ok(list);
    }


    /**
     * 获取所有的任务列表
     * @param paging Paging
     *        title String
     *        classId Integer
     * return list
     * */
    @PostMapping("task_list")
    public ApiResult getTaskList(@BeanValid @RequestBody Paging paging,
                                 @SimpleParam("title") String title,
                                 @SimpleParam("classId") Integer classId,
                                 @SimpleParam("timeType") Integer timeType,
                                 @SimpleParam("returnType") Integer returnType){
       returnType = returnType != null?returnType:0;
       Map<String,Object> list = taskService.getAllTaskList(paging,classId,title,timeType,returnType);
       return ApiResultUtil.ok(list);
    }


    /**
     * 获取任务详情
     * @param id Integer
     * return Object
     * */
    @PostMapping("task_detail")
    public ApiResult getTaskDetail(@SimpleParam("id") Integer id){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        Map<String,Object> list = taskService.getTaskDetail(id);
        return ApiResultUtil.ok(list);
    }


    /**
     * 编辑任务
     * @param taskDO TaskDO
     * return boolean
     * */
     @PostMapping("edit_task")
     public ApiResult edtiTask(@BeanValid @RequestBody TaskDO taskDO){
         if(taskDO.getClassId() == null){
             throw BusinessException.of(ErrorMessageEnum.CLASS_NOT_FOUND);
         }
       taskService.editTask(taskDO);
       return ApiResultUtil.ok(SuccessMessageEnum.OK);
     }


    /**
     * 发布任务
     * @param taskInputDTO TaskInputDTO
     * return boolean
     * */
    @PostMapping("publish")
    public ApiResult publishTask(@BeanValid @RequestBody TaskInputDTO taskInputDTO){
        //判断该用户是否有全新
        taskService.publishTask(taskInputDTO);
        return ApiResultUtil.ok(SuccessMessageEnum.PUBLISH_SUCCESS);
    }



    /**
     * 获取任务详情
     * @param id Integer
     * return Object
     * */
    @PostMapping("getTaskInfo")
    public ApiResult getTaskInfo(@SimpleParam("id") Integer id){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        Map<String,Object> list = taskService.getTaskInfo(id);
        return ApiResultUtil.ok(list);
    }


    /**
     * 获取转发的秘钥
     * @param url
     * return Object
     * */
    @PostMapping("getSignature")
    public ApiResult getSignature(@SimpleParam("url") String url){
        WxJsapiSignature jsapiSignature;
        final WxMpService wxMpService = WxMpConfiguration.getMpServices().get(appId);
        Map<String,Object> list = new HashMap<>();
        try {
            jsapiSignature = wxMpService.createJsapiSignature(url);
            log.info("jsap{}",jsapiSignature);
            list.put("appId",jsapiSignature.getAppId());
            list.put("nonceStr",jsapiSignature.getNonceStr());
            list.put("timestamp",String.valueOf(jsapiSignature.getTimestamp()));
            list.put("nature",jsapiSignature.getSignature());
            list.put("url",jsapiSignature.getUrl());
        } catch (WxErrorException e) {
            e.printStackTrace();
            System.out.println(e.getError().getErrorMsg());
            return null;
        }
        return ApiResultUtil.ok(list);
    }




}
